package com.rjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rjy.entity.Query;
import com.rjy.entity.Student;
import com.rjy.entity.Teacher;
import com.rjy.entity.User;
import com.rjy.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//2
@CrossOrigin(origins = "*")
@RestController
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;

    @CrossOrigin(origins = "*")
    @RequestMapping("/insertStudent")
    public Integer insertStudent(Student student){
        studentMapper.insertStudent(student);
        System.out.println("=================");
        return 1;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping("/updateStudent")
    public Integer updateStudent(Student student){
        int i= studentMapper.updateById(student);
        return i;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping("/queryAllStudentByPage")
    public Map<String,Object> queryAllStudentByPage(Query query){
        Page<Student> pages = new Page<>(query.getPage(), query.getLimit());
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(query.getId()!=null && !"".equals(query.getId())){
            queryWrapper.eq("id",query.getId());
        }
        if(query.getName()!=null && !"".equals(query.getName())){
            queryWrapper.like("name","%"+query.getName()+"%");
        }
        if(query.getSex()!=null && !"".equals(query.getSex())){
            queryWrapper.eq("sex",query.getSex());
        }
        if(query.getHiredate()!=null && !"".equals(query.getHiredate())){
            queryWrapper.eq("hiredate",query.getHiredate());
        }
        Long count = studentMapper.selectCount(queryWrapper);
        studentMapper.selectPage(pages, queryWrapper);
        List<Student> list = pages.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);
        return map;
    }
    @CrossOrigin(origins = "*")
    @RequestMapping("deleteStudent")
    public Integer deleteStudent(@RequestBody List<Integer> ids){
        int cout= studentMapper.deleteBatchIds(ids);
        return cout;
    }
}
